/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.insta.browser.service.history;

import fr.insta.browser.domain.IHistoryEntry;
import fr.insta.webview.factory.WebObjectFactory;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.ImageIcon;

/**
 * Implementation d'un service de gestion d'historique
 *
 * @author Maarek
 * @version $Id: $Id
 */
public class HistoryService implements IHistoryService {

    final protected List<ChangeHistoryListener> listeners = new ArrayList<>();
    final protected List<IHistoryEntry> entries = new ArrayList<>();

    /** {@inheritDoc} */
    @Override
    public void addEntry(String url, String title, ImageIcon icon) {
        IHistoryEntry e = (IHistoryEntry) WebObjectFactory.getHistoryFactory().createNewWebObject(title, url);
        entries.add(e);
        fireAddEntry(e);
        //System.out.println(e.getUrl()+" - "+e.getTitle());
    }

    /** {@inheritDoc} */
    @Override
    public Iterator<IHistoryEntry> iterator() {
        return entries.iterator();
    }

    /**
     * <p>fireChangeCurrentEntry.</p>
     *
     * @param entry a {@link fr.insta.browser.domain.IHistoryEntry} object.
     */
    protected void fireChangeCurrentEntry(IHistoryEntry entry) {
        for (ChangeHistoryListener listener : listeners) {
            listener.onChangeCurrentEntry(entry);
        }
    }

    /**
     * <p>fireAddEntry.</p>
     *
     * @param entry a {@link fr.insta.browser.domain.IHistoryEntry} object.
     */
    protected void fireAddEntry(IHistoryEntry entry) {
        for (ChangeHistoryListener listener : listeners) {
            listener.onAddEntry(entry);
        }
    }

    /** {@inheritDoc} */
    @Override
    public void addListener(ChangeHistoryListener listener) {
        listeners.add(listener);
    }

    /** {@inheritDoc} */
    @Override
    public void removeListener(ChangeHistoryListener listener) {
        listeners.remove(listener);
    }
}
